<template>
	<view>
		<view class="banner"></view>
		<view class="news">最新疫情：</view>
		<view class="recentNumber">
			<view class="icbar confirm">
				<view class="number" style="color: #d81d1b;">{{back.confirmedCount}}</view>
				<view class="text">全国确诊</view>
			</view>
			<view class="icbar suspect">
				<view class="number" style="color: #f7ab1a;">{{back.suspectedCount}}</view>
				<view class="text">疑似病例</view>
			</view>
			<view class="icbar cure">
				<view class="number" style="color: #178b50;">{{back.curedCount}}</view>
				<view class="text">治愈人数</view>
			</view>
			<view class="icbar dead">
				<view class="number" style="color: #66666c;">{{back.deadCount}}</view>
				<view class="text">死亡人数</view>
			</view>
		</view>
		<histogram-chart v-if="canvasFlag" :dataAs="histogramData2" canvasId="ht1" labelKey="label" valueKey="value" :yAxisAs="{
				formatter: {
					type: 'number' //type:number percent String,额外参数:fixed:NUmber,name:String
				}
			}" />
		<view style="text-align: center;line-height: 40px;">确诊人数统计图</view>
		<view class="list" v-for="(item,i) in back.list" :key="i">
			<view class="place" @tap="listTap(i)">
				<text class="text">{{item.provinceShortName}}</text>
				<view class="icon iconfont" :class="{active:listTapTarget == i}">&#xe65e;</view>
				<text class="msg">确诊{{item.confirmedCount}}例，治愈{{item.curedCount}}例，死亡{{item.deadCount}}例</text>
			</view>
			<view class="list-child-box" v-show="listTapTarget == i">
				<view class="child-place" v-for="(item2,j) in item.cities" :key="j" style="background-color: rgba(248,248,248,0.3);">
					<text class="text" style="font-size: 24upx;">{{item2.cityName}}</text>
					<text class="msg" style="font-size: 24upx;">确诊{{item2.confirmedCount}}例，治愈{{item2.curedCount}}例，死亡{{item2.deadCount}}例</text>
				</view>
			</view>
		</view>
		<!-- 占位 -->
		<view class="seat"></view>
	</view>
</template>
<script>
	import HistogramChart from '../../components/stan-ucharts/HistogramChart.vue';
	export default {
		name: 'Index',
		components: {
			HistogramChart
		},
		data() {
			return {
				// 是否加载canvas
				canvasFlag: false,
				// 当前点击的列表
				listTapTarget: null,
				back: {
					// 确认
					confirmedCount: "",
					// 疑似
					suspectedCount: "",
					// 治愈
					curedCount: "",
					// 死亡
					deadCount: "",
					list: []
				},
				histogramData2: {
					//柱状图Compent  //label应为series value 应为
					label: [],
					value: [{
						name: '确诊人数 单位：人',
						data: []
					}]
				},

			}
		},
		onLoad() {
			this.getData();
			//获取疑似病例
			uni.request({
				url: "http://api.tianapi.com/txapi/ncov/index",
				data: {
					key: "d9a5fe28d47299e04f121b8a909e9e86"
				},
				success: (res) => {
					this.back.suspectedCount = res.data.newslist[0].desc.suspectedCount
				}
			})
		},
		methods: {
			// 点击列表
			listTap(i) {
				if (this.listTapTarget == i) {
					this.listTapTarget = null;
				} else {
					this.listTapTarget = i;
				}


			},
			getData() {
				uni.showLoading({
					title: "加载中..."
				})
				uni.request({
					url: "http://api.tianapi.com/txapi/ncovcity/index",
					data: {
						key: "d9a5fe28d47299e04f121b8a909e9e86"
					},
					success: (res) => {
						console.log(res.data.newslist);
						uni.hideLoading();
						this.back.list = res.data.newslist
						res.data.newslist.forEach((val) => {
							this.histogramData2.label.push(val.provinceShortName);
							this.histogramData2.value[0].data.push(val.confirmedCount);
							this.back.confirmedCount = Number(this.back.confirmedCount + val.confirmedCount);
							this.back.suspectedCount = Number(this.back.suspectedCount + val.suspectedCount);
							this.back.curedCount = Number(this.back.curedCount + val.curedCount);
							this.back.deadCount = Number(this.back.deadCount + val.deadCount);
							this.canvasFlag = true;
						})
					}
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.banner {
		width: 100%;
		height: 340upx;
		background: url(http://ww1.sinaimg.cn/large/006oTPUvly1gba4xazr67j30wa0dh17j.jpg) no-repeat;
		background-size: contain;
	}

	.news {
		font-size: 32upx;
		padding-left: 28upx;
		margin-bottom: 20upx;
	}

	.recentNumber {
		padding: 0 40upx;

		.icbar {
			width: 25%;
			height: 100upx;
			display: inline-block;
			text-align: center;
			color: #222;
			font-size: 24upx;

			.number {
				text-align: center;
				font-size: 44upx;
			}
		}
	}

	.list {
		width: 90%;
		margin: 16upx auto;
		background: #f8f8f8;
		border-radius: 12upx;
		line-height: 80upx;
		padding: 0 20upx;
		box-sizing: border-box;
		position: relative;

		.place .text {
			font-size: 32upx;
			color: #005dff;
		}

		.icon {
			position: absolute;
			right: 20upx;
			top: 0;
		}

		.icon.active {
			transform: rotate(90deg);
		}

		.msg {
			font-size: 28upx;
			color: #737373;
			margin-left: 14upx;
		}

		.list-child-box {
			width: 100%;

			.list-child {
				width: 100%;
				height: 60upx;
				background: #C0C0C0;
				margin: 12upx 0;
				border-radius: 12upx;
			}
		}
	}
</style>
